<template>
  <div class="login-container">
    <el-form :model="ruleForm"
             status-icon
             ref="ruleForm"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">北航艺术团管理系统</h3>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="ruleForm.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-checkbox
          v-model="ruleForm.remember_password"
      >记住密码
      </el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="primary"
                   style="width:100%;
                   margin-top: 20px"
                   @click="handleSubmit"
                   :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      logining: false,
      ruleForm: {
        username: '',
        password: '',
        remember_password: false
      },

    }
  },
  methods: {
    handleSubmit() {
      let couldLogin = false
      if (this.ruleForm.remember_password) {
        //传入账号名，密码，和保存天数3个参数
        this.set_account_cookies(this.ruleForm.username, this.ruleForm.password, true, 7);
      }else {
        //清空Cookie
        this.clear_account_cookies();
      }
      axios.post(this.$store.state.post_site_dir.login,
          {
            username: this.ruleForm.username,
            password: this.ruleForm.password
          })
          .then((res) => {
            if (res.data.error_code === 0) {
              if (res.data.key === -1)
                res.data.key = 0xffffffff
              this.$store.commit('set_key', res.data.key)
              this.$store.commit('set_perm_code', res.data.perm_code)
              this.$store.commit('set_cookies')
              this.$router.push({path: '/home'})
            } else {
              this.$alert('用户名或密码错误', '登陆失败', {
                confirmButtonText: 'ok'
              })
            }
          })
    },
    set_account_cookies(c_name, c_pwd, remember_password, exdays, key) {
      // key是后端传给前端的key
      let exdate = new Date(); //获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
      //字符串拼接cookie
      window.document.cookie = "username" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
      window.document.cookie = "password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
      // 调用setCookie说明一定是要记住密码的
      remember_password = remember_password ? 'true' : 'false';
      if (remember_password)
      window.document.cookie = 'remember_password=' + remember_password + ";path=/;expires=" + exdate.toGMTString();
      },
    get_account_cookies() {
      if (document.cookie.length > 0) {
        let arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
        for (let i = 0; i < arr.length; i++) {
          let arr2 = arr[i].split('='); //再次切割
          //判断查找相对应的值
          if (arr2[0] === 'username') {
            this.ruleForm.username = arr2[1]; //保存到保存数据的地方
          } else if (arr2[0] === 'password') {
            this.ruleForm.password = arr2[1];
          } else if (arr2[0] === 'remember_password') {
            this.ruleForm.remember_password = arr2[1] === 'true';
          }
        }
      }
    },
    clear_account_cookies: function() {
      this.set_account_cookies("", "", false, -1); //修改2值都为空，天数为负1天就好了
    }
  },
  mounted() {
    this.get_account_cookies();
  }
};
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100%;
}

.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

label.el-checkbox.rememberme {
  margin: 0px 0px 15px;
  text-align: left;
}
</style>

